package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.CoinType;
import cn.net.shuxing.yifu.common.bean.OpenPriceHistory;
import cn.net.shuxing.yifu.common.bean.Permission;
import cn.net.shuxing.yifu.common.bean.PermissionGroup;
import cn.net.shuxing.yifu.common.mapper.OpenPriceHistoryMapper;
import cn.net.shuxing.yifu.common.mapper.PermissionMapper;
import cn.net.shuxing.yifu.common.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.net.shuxing.yifu.common.util.StringUtils;
import redis.clients.jedis.Jedis;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class OpenPriceHistoryService extends BaseService<OpenPriceHistory, OpenPriceHistoryMapper> {

    @Autowired
    private CoinRatioService coinRatioService;

    @Autowired
    private JedisUtil jedisUtil;

    public BigDecimal getByCoinTypeId(Integer coinTypeId) {
        List<OpenPriceHistory> openPriceHistories = getsByCoinTypeId(coinTypeId);
        if (openPriceHistories == null || openPriceHistories.isEmpty())
            return new BigDecimal(0);
        return openPriceHistories.get(openPriceHistories.size() - 1).getOpenPrice();
    }

    @Override
    public int insert(OpenPriceHistory bean) {
        bean.setCreateDate(LocalDate.now());
        bean.setUpdateTime(LocalDateTime.now());
        super.insert(bean);
        //Jedis //jedis = jedisUtil.getJedis();
        jedisUtil.set(RedisKeyCreator.openPrice(bean.getCoinTypeId()), bean.getOpenPrice().toString());
        //jedisUtil.close();
        return bean.getId();
    }

    public int insert(Integer coinTypeId, BigDecimal openPrice) {
        OpenPriceHistory openPriceHistory = new OpenPriceHistory();
        openPriceHistory.setOpenPrice(openPrice);
        openPriceHistory.setCoinTypeId(coinTypeId);
        return insert(openPriceHistory);
    }

    public int insert(Integer coinTypeId) {
        BigDecimal gain = coinRatioService.calculateCoinGain(coinTypeId);
        //Jedis //jedis = jedisUtil.getJedis();
        BigDecimal openPrice = new BigDecimal(jedisUtil.get(RedisKeyCreator.openPrice(coinTypeId)));
        //jedisUtil.close();
        openPrice = openPrice.multiply(gain);
        return insert(coinTypeId, openPrice);
    }

    /**
     * 手动设置开盘价
     */
    public void setOpenPrice(Integer coinTypeId, BigDecimal openPirce) {
        LocalDate today = LocalDate.now();
        TimeExample example = new TimeExample(OpenPriceHistory.class);
        example.equals("createDate", today)
                .equals("coinTypeId", coinTypeId);
        OpenPriceHistory openPriceHistory = getByExample(example);
        openPriceHistory.setOpenPrice(openPirce);
        update(openPriceHistory);
    }

    @Override
    public int update(OpenPriceHistory bean) {
        if (super.update(bean) < 1) {
            return -1;
        } else {
            jedisUtil.set(RedisKeyCreator.openPrice(bean.getCoinTypeId()), bean.getOpenPrice().toString());
            return 1;
        }
    }


    public List<OpenPriceHistory> getsByCoinTypeId(Integer coinTypeId) {
        CommonExample example = new CommonExample(OpenPriceHistory.class);
        example.equals("coinTypeId", coinTypeId);
        return getsByExample(example);
    }

    public BigDecimal getOpenPrice(Integer coinTypeId) {
        //Jedis //jedis = jedisUtil.getJedis();
        String s = jedisUtil.get(RedisKeyCreator.openPrice(coinTypeId));
        //jedisUtil.close();
        BigDecimal newestPrice = new BigDecimal(0);
        if (!StringUtils.hasText(s)) {
            newestPrice = this.getByCoinTypeId(coinTypeId);
            if (newestPrice.compareTo(new BigDecimal(0)) <= 0) {
                newestPrice = getByCoinTypeId(coinTypeId);
            }
            jedisUtil.set(RedisKeyCreator.openPrice(coinTypeId), newestPrice.toString());
        } else {
            newestPrice = new BigDecimal(s);
        }

        return newestPrice;
    }
}
